<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Links_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
	function add($data = array()){
		$this->mycache->delete('get_links_list_index');
		$this->db->insert('rsh_links', $data);
		return $this->db->insert_id();
	}
	

	function update($id, $data = array()){
		$this->mycache->delete('get_links_list_index');
		$this->db->where('id', $id);
		$this->db->update('rsh_links', $data);
		return $this->db->affected_rows();
	}
	
	function get_links_obj($id){
		$select = "id,url,webname,logoimg,forder,desc,type";
		$this->db->select($select);
		$this->db->where(array('id'=>$id));
		$query = $this->db->get('rsh_links');
		return $query->row_array();
	}
	
	function get_links_list($data=array()){
		//封装查询内容
		$select = "id, url, webname, logoimg, forder";
		
		//封装条件
		$_where = " where is_del=0 ";
		
		if($data['type']==2){
			$_where .=' and type=2';
		}else{
			$_where .=' and type=1';
		}
		
		
		//封装sql
		$sql_result = "select $select from rsh_links";
		$sql_result .= $_where;
		$query = $this->db->query($sql_result);
		$data['links_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_links";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['links_count'] = $query->row()->count;
		return $data;
	}
	
	function get_links_list_index(){
		$mkey = "get_links_list_index";
		//$this->mycache->delete($mkey);
		$ret = $this->mycache->get_array($mkey);
		if($ret != false){
			return $ret;
		}
		//封装查询内容
		$select = "id, url, webname, logoimg, forder";
		
		//封装条件
		$_where = " where is_del=0 ";
		$_order = " order by forder asc,id asc";
		//封装sql
		$sql_result = "select $select from rsh_links";
		$sql_result .= $_where.$_order;
		$query = $this->db->query($sql_result);
		$links_list = $query->result_array();
		$this->mycache->set_array($mkey,$links_list);
		return $links_list;
	}
	
	
	//****** by liub 20141231 
	function get_links_list_by_type($type){
		$mkey = "links_list_valid";
		$select = "id, url, webname, logoimg, forder";
		$_where = " where is_del=0 ";
		if($type==2){
			$_where .=' and type=2';
		}else{
			$_where .=' and type=1';
		}
		$sql_result = "select $select from rsh_links";
		$sql_result .= $_where;
		$query = $this->db->query($sql_result);
		$links_list = $query->result_array();
		return $links_list;
	}
	
}